<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty {pp_js} function plugin
 *
 * File:       function.pp_js.php<br>
 * Type:       function<br>
 * Name:       pp_js<br>
 * Date:       24.Feb.2003<br>
 * Purpose:    Prints out a list of text input types<br>
 * Input:<br>
 *           - name       (optional) - string default "text"
 *           - values     (required) - array
 *           - options    (optional) - associative array
 *           - checked    (optional) - array default not set
 *           - separator  (optional) - ie <br> or &nbsp;
 *           - output     (optional) - the output next to each text button
 *           - assign     (optional) - assign the output as an array to this variable
 * Examples:
 * <pre>
 * {pp_js values=$ids output=$names}
 * {pp_js values=$ids name='box' separator='<br>' output=$names}
 * {pp_js values=$ids checked=$checked separator='<br>' output=$names}
 * </pre>
 * @link http://smarty.php.net/manual/en/language.function.html.texts.php {pp_js}
 *      (Smarty online manual)
 * @author     Christopher Kvarme <christopher.kvarme@flashjab.com>
 * @author credits to Monte Ohrt <monte at ohrt dot com>
 * @version    1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_pp_js($params, &$smarty)
{
	require_once $smarty->_get_plugin_filepath('shared','escape_special_chars');
	
	$file = NULL;
	
	foreach($params as $_key => $_val) {
		switch($_key) {
			case 'file':
				$$_key = (string)$_val;
				break;
			
			default:
				if( !is_array($_val) ) {
					$extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
				} else {
					$smarty->trigger_error("pp_js: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
				}
				break;
		}
	}
	
	//
	$js_all = array();
	if( CONST_JS_PACKER == 'true' ) {
		$js_all = explode('|', CONST_JS_ALL);
	}
	//
	if( strstr($file, ',') !== false ) {
		$file = explode(',', $file);
	} elseif( !is_array($file) ) {
		$file = array($file);
	}
	foreach($file as $k => $v ) {
		if( substr($v, 0, strlen('pjs.')) == 'pjs.' ) {
			//
		}
		if( substr($v, -3) != '.js' ) {
			$v = $v . '.js';
		}
		if( in_array($v, $js_all) ) {
			return '';	
		}
		$_html_result .= '<script language="javascript" type="text/javascript" src="' . SYS_RT_URL_JS . $v . '"></script>' . "\n";
	}

	return $_html_result;
}
//
?>
